package model

import (
	"errors"
	"gorm.io/gorm"
)

type TableDao interface {
	GetTables(tableNamesStr string, databaseName string) (*[]Table, error)
	GetList() (*[]Table, error)
}

type defaultTableDao struct {
	engine *gorm.DB
}

func NewTableDao(engine *gorm.DB) *defaultTableDao {
	return &defaultTableDao{
		engine: engine,
	}
}

func (d defaultTableDao) GetList() (*[]Table, error) {
	s := new([]Table)
	var err error
	//这里写逻辑
	return s, err
}

func (d defaultTableDao) GetTables(tableNamesStr string, databaseName string) (*[]Table, error) {
	// 获取指定表信息
	tables := new([]Table)
	if databaseName == "" {
		return tables, errors.New("数据库名称不能为空")
	}
	if tableNamesStr == "''" {
		d.engine.Raw("SELECT TABLE_NAME as Name,TABLE_COMMENT as Comment FROM information_schema.TABLES " +
			"WHERE table_schema='" + databaseName + "';").Find(&tables)
	} else {
		d.engine.Raw("SELECT TABLE_NAME as Name,TABLE_COMMENT as Comment FROM information_schema.TABLES " +
			"WHERE TABLE_NAME IN (" + tableNamesStr + ") AND " +
			"table_schema='" + databaseName + "';").Find(&tables)
	}
	return tables, nil
}
